package com.example.hp.serviceproject;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.File;

public class MusicActivity extends AppCompatActivity {
    private String[] fs = null;
    private MediaPlayer player;
    private  String mp3Path="";
    private Boolean isPause = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/mp3/";
        File mp3File = new File(path);
        fs = mp3File.list();

        ListView lv = (ListView) findViewById(R.id.musicList);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,fs);
        lv.setAdapter(adapter);
        player = new MediaPlayer();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent();
                intent.putExtra("action","play");
                intent.putExtra("music",fs[position]);
                intent.setAction("music");
                intent.setPackage(MusicActivity.this.getPackageName());
                startService(intent);
            }
        });
    }

    public void play(View view) throws  Exception{
        Intent intent=new Intent();
        intent.putExtra("action","play");
        //intent.setClass(this,MusicService.class);
        intent.setAction("music");
        intent.setPackage(this.getPackageName());
        startService(intent);

    }

    public void pause(View view) {

        Intent intent=new Intent();
        intent.putExtra("action","pause");
        intent.setPackage(this.getPackageName());
        //intent.setClass(this,MusicService.class);
        intent.setAction("music");
        startService(intent);
    }
}
